package leetcode.primary;

public class T238 {
    public int[] productExceptSelf(int[] nums) {
        int[] ans = new int[nums.length];
        int prefix = 1;
        for (int i = 0; i < nums.length; i++) {
            prefix*=nums[i];
            ans[i] = prefix;
        }
        int suffix = 1;
        for (int i = nums.length-1; i >0 ; i--) {
            ans[i] = ans[i-1]*suffix;
            suffix*=nums[i];
        }
        ans[0]=suffix;
        return ans;
    }

    public static void main(String[] args) {
        T238 t238 = new T238();
        int[] ans = t238.productExceptSelf(new int[]{1, 2, 3, 4,5});
        for (int i : ans) {
            System.out.print(i+" ");
        }
    }
}
